package com.pro.service.impl;

import com.pro.entry.JsBeforeUser;
import com.pro.mapper.LoginMapper;
import com.pro.service.LoginService;
import com.pro.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author 谭金诚
 * @Description TODO
 * @Date 2022/4/7 9:24
 * @Version 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public JsBeforeUser getLogin(JsBeforeUser beforeUser) {
        String password = beforeUser.getPassword();
        //md5加密
        password = MD5Util.md5(password);
        beforeUser.setPassword(password);
        JsBeforeUser login = loginMapper.getLogin(beforeUser);
        return login;
    }

    @Override
    public JsBeforeUser getLoginByPhone(String phone) {
        JsBeforeUser loginByPhone = loginMapper.getLoginByPhone(phone);
        return loginByPhone;
    }

    @Override
    @Transactional
    public JsBeforeUser getLoginByWX(JsBeforeUser beforeUser) {
        JsBeforeUser loginByWX = loginMapper.getLoginByWX(beforeUser);
        if (loginByWX == null){
            boolean flag = loginMapper.addLoginByWX(beforeUser);
            JsBeforeUser loginByWXTwo = loginMapper.getLoginByWX(beforeUser);
            return loginByWXTwo;
        }
        return loginByWX;
    }

    @Override
    public boolean updateUserPWD(String phone, String password) {
        password = MD5Util.md5(password);
        boolean flag = loginMapper.updateUserPWD(phone, password);
        return flag;
    }

    @Override
    public boolean addBeforeUser(JsBeforeUser beforeUser) {
        String password = MD5Util.md5(beforeUser.getPassword());
        beforeUser.setPassword(password);
        beforeUser.setStatus(2);//2 账号正常
        boolean flag = loginMapper.addBeforeUser(beforeUser);
        return flag;
    }
}
